package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public void create(BedDto dto) {
        Bed bed = BeanUtil.toBean(dto, Bed.class);
        //设置创建时间，更新时间，创建人，更新人，状态
        bed.setCreateBy(1671403256519078138L); //admin的id
        bed.setUpdateBy(1671403256519078138L); //admin的id
        bed.setBedStatus(0);//0未入住
        //添加床位到数据库
        try {
            bedMapper.insert(bed);
        }catch (Exception e){
            throw new RuntimeException(String.valueOf(BasicEnum.BED_INSERT_FAIL));
        }

    }

    @Override
    public BedVo read(Long id) {
        Bed bed = bedMapper.selectById(id);
        BedVo bedVo = new BedVo();
        if (bed != null)
            BeanUtils.copyProperties(bed, bedVo);
        return bedVo;
    }

    @Override
    public void update(BedDto dto) {
        Bed bed = new Bed();
        BeanUtils.copyProperties(dto, bed);
        bedMapper.updateById(bed);
    }

    @Override
    public void delete(Long id) {
        bedMapper.deleteById(id);
    }
}

